package com.rois.librois.libRoISMessageStorage;

import java.util.Vector;

/**
 * Data holder class for registered message.
 * This object holds a list of:
 * - message types
 * - registered IDs (i.e. subscribe ID for Event, bound component ID for Command)
 * - exchanged messages
 *
 * @author Miki Sato <miki.sato.1225@gmail.com>
 *
 *    The MIT licence
 *
 *    Copyright (C) 2011
 *      ATR Intelligent Robotics and Communication Laboratories, Japan
 *
 *    Permission is hereby granted, free of charge, to any person obtaining a copy
 *    of this software and associated documentation files (the "Software"), to deal
 *    in the Software without restriction, including without limitation the rights
 *    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 *    of the Software, and to permit persons to whom the Software is furnished to do so,
 *    subject to the following conditions:
 *
 *    The above copyright notice and this permission notice shall be included in all
 *    copies or substantial portions of the Software.
 *
 *    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 *    INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 *    PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *    OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 *    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

public class RoIS_RegisteredTypes {
	private static final int DEFAULT_TYPE_MAXSIZE = 100;
	private static final int DEFAULT_MSG_MAXSIZE = 100;
	public int size;
	public int max_size;
	public int max_message_size;
	private int reference_count;
	private int message_count;
	public Vector<String> type  = new Vector<String>();
	public Vector<String> referenceid = new Vector<String>();
	public Vector<RoIS_ExchangedMessages> messages = new Vector<RoIS_ExchangedMessages>();

	/**
	 * create data holder.
	 */
	public RoIS_RegisteredTypes()
	{
		max_size = DEFAULT_TYPE_MAXSIZE;
		max_message_size = DEFAULT_MSG_MAXSIZE;
		clear();
	}

	/**
	 * clear all data.
	 */
	public void clear()
	{
		size = 0;
		reference_count = 0;
		message_count = 0;
		type.clear();
		referenceid.clear();
		messages.clear();
	}

	/**
	 * add message at the end of list.
	 * @param _type message type
	 * @param _referenceid
	 */
	public void push_back(
		String _type,
		String _referenceid
	)
	{
		type.add(_type);
		referenceid.add(_referenceid);
		RoIS_ExchangedMessages _messages = new RoIS_ExchangedMessages();
		_messages.clear();
		_messages.max_size = max_message_size;
		messages.add(_messages);
		size++;

		if(size>max_size)
		{
			erase(0);
		}
	}

	/**
	 * remove message at the end of list.
	 */
	public void pop_back()
	{
		if(size>0)
		{
			type.remove(type.size()-1);
			referenceid.remove(referenceid.size()-1);
			messages.remove(messages.size()-1);
			size--;
		}
	}

	/**
	 * remove specified message from list
	 * @param index index of the message
	 */
	public void erase(
			int index
		)
	{
		if(size>0)
		{
			type.remove(index);
			referenceid.remove(index);
			messages.remove(index);
			size--;
		}
	}

	/**
	 * create new reference ID.
	 * @return new reference ID
	 */
	public String createReferenceID()
	{
		reference_count++;
		String strRes = String.valueOf(reference_count);
		return strRes;
	}

	/**
	 * create new message ID.
	 * @return new message ID.
	 */
	public String createMessageID()
	{
		message_count++;
		String strRes = String.valueOf(message_count);
		return strRes;
	}

	/**
	 * get message index related to reference ID.
	 * @param _referenceid reference ID of the message
	 * @return index of the message
	 */
	public int getIndexByReferenceId(
		String _referenceid
	)
	{
		int iRes = -1;
		for(int i=0; i<size; i++)
		{
			if(referenceid.get(i).matches(_referenceid))
			{
				iRes = i;
				break;
			}
		}
		return iRes;
	}

	/**
	 * get message index related to message ID.
	 * @param _messageId message ID of the message
	 * @return index of the message
	 */
	public int getIndexByMessageId(
		String _messageId
	)
	{
		int iRes = -1;
		for(int i=0; i<size; i++)
		{
			int msg_size = messages.get(i).size;
			for(int j=0; j<msg_size; j++)
			{
				if(messages.get(i).id.get(j).matches(_messageId))
				{
					iRes = i;
					break;
				}
			}
		}
		return iRes;
	}

	/**
	 * erase message related to reference ID.
	 * @param _referenceid reference ID of the message.
	 */
	public void eraseByReferenceID(
		String _referenceid
	)
	{
		for(int i=0; i< size; i++)
		{
			if(referenceid.get(i).matches(_referenceid))
			{
				erase(i);
				break;
			}
		}
	}
}
